<?php
namespace app\app\controller\v1;
use think\Db;
use app\app\util\OnlineUtils;

class LineController extends  BaseController{

    /**
     * @return array
     * 首页推荐线路
     */
    public function LineHot()
    {
//        $pageindex = input('post.pageindex');
//        $pagesize = input('post.pagesize');
//        if( $pageindex <= 0 ){
//            $length = 0;
//        }else{
//            $length = $pagesize*$pageindex;
//        }
        $class_id = input('post.class_id');
        $data = Db::name('line')->alias('l')
            ->join('line_class lc','l.class_id = lc.id','left')
            ->where(['l.status'=>1,'l.class_id'=>$class_id])
            ->order('l.sort desc')->field('l.id,l.cover,l.title,l.price,l.ware_type_id,l.sort,lc.line_class_name')
           ->limit(9)
            ->select();
        return json_code(200,'success',$data);
    }

    /**
     * @return array
     * 全部线路列表
     */
    public function LineList()
    {
        $pageindex = input('post.pageindex');
        $pagesize = input('post.pagesize');
        $class_id = input('post.class_id');
        if( $pageindex <= 0 ){
            $length = 0;
        }else{
            $length = $pagesize*$pageindex;
        }
        $data = Db::name('line')->where(['status'=>1,'class_id'=>$class_id])
            ->field('id,title,price,cover')->limit($length,$pagesize)->select();
//        foreach ( $data as $a => $v){
//            $data[$a]['cover'] = Db::name('slider')->where(['status'=>1,'line_id'=>$v['id']])->field('img')->find();
//            $data[$a]['cover'] = $data[$a]['cover']['img'];
//        }
        return json_code(200,'success',$data);
    }


    /**
     * @return array
     * 线路详情
     */
    public function LineDetail()
    {
        $user_id = OnlineUtils::getUserIdBySid();
        $id = input('get.id');
        $user = Db::name('user')->field('tid')->find($user_id);
//        if( input('get.user_id') && input('get.user_id') != 0 ){
//            if($user['tid'] == 0){
//                Db::name('user')->update(array('id'=>$user_id,'tid'=>input('get.user_id')));
//            }
//        }
        $is_collection = Db::name('collection')->where(['line_id'=>$id,'user_id'=>$user_id,'status'=>1])->find();

        $data = Db::name('line')->where(['status'=>1])->find($id);
        //轮播图
        $data['slider'] = Db::name('slider')->field('id,img')->where(['status'=>1,'line_id'=>$id])->find();
        $data['slider'] = explode(',',  $data['slider']['img']);
        //线路配置
        $config = Db::name('line_config')->field('group_concat(title) title')->where('id','in',$data['config'])->find();
        $data['config'] = explode(',',$config['title']);
        //班期
        $data['flight'] = date('m-s',$data['flight']);
        //关联线路
        $data['relate'] = $data['relate'].','.$id;
        $data['relate'] = explode(',',$data['relate']);
        for($i=0;$i<count($data['relate']);$i++){
            $flight = Db::name('line')->field('flight,price')->where('status','eq',1)->find($data['relate'][$i]);
            $flights[] = $flight;
        }
        foreach ( $flights as $a => $v){
            $flights[$a]['flight'] = date('m-s',$v['flight']);
        }
        $data['relate'] = $flights;
        if($is_collection){
            $data['is_collection'] = 1;
        }else{
            $data['is_collection'] = 0;
        }
        return json_code(200,'success',$data);

    }

    /**
     * @return array
     * 线路详情配置
     */
    public function LineDetailConfig()
    {
        $id = input('get.id');
        $config = Db::name('line')->find($id);
        $data = explode(',',$config['config']);
        for($i=0;$i<count($data);$i++){
            $config = Db::name('line_config')->field('title,content')->where('status','eq',1)->find($data[$i]);
            $configs[] = $config;
        }
        return json_code(200,'success',$configs);
    }

    /**
     * @return array
     * 添加购物车
     */
    public function AddShoppingCart()
    {
        $user_id = OnlineUtils::getUserIdBySid();
        $line_id = input('get.line_id');
        $data = array('user_id'=>$user_id,'line_id'=>$line_id,'created_at'=>time());
        //判断该用户是否已经添加此线路
        $line_Record = Db::name('shopping_cart')->where(['user_id'=>$user_id,'line_id'=>$line_id])->find();
        if( $line_Record ){
            //修改数据库
            Db::query('update t_shopping_cart set num = num + 1 where user_id='.$user_id.' and line_id='.$line_id);
            return json_code(200,'添加购物车成功');
        }elseif ( !$line_Record ){
            //添加数据库
            if(  Db::name('shopping_cart')->insertGetId($data)  ){
                return json_code(200,'添加购物车成功');
            }
            return json_code(402,'添加购物车失败');
        }
    }
    
    
    /**
     * 搜索线路
     */

    public function searchLine()
    {
        $name = input('post.name');
        $data = Db::name('line')->where('title like  "%' . $name . '%" and status = 1')->select();
        return json_code(200,'success',$data);
    }
}